package com.abhinav.hibernate;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import com.abhinav.hibernate.domain.Category;
import com.abhinav.hibernate.domain.Stock;
import com.abhinav.hibernate.domain.StockDetail;

public class AppJPA {
	public static void main(String[] args) {
		EntityManagerFactory enityMgrFactry;
		EntityManager entityManager;
		//EntityTransaction entityTrans;
		enityMgrFactry = Persistence.createEntityManagerFactory("CustomerService");
		entityManager = enityMgrFactry.createEntityManager();
		entityManager.getTransaction().begin();

		Stock stock = new Stock();
        stock.setStockCode("7152");
        stock.setStockName("ABHISHEK");
        
        Category category1 = new Category("CONSUMER", "CONSUMER COMPANY");
        Category category2 = new Category("INVESTMENT", "INVESTMENT COMPANY");
        
        entityManager.persist(category1);
        entityManager.persist(category2);
		
        Set<Category> categories = new HashSet<Category>();
        categories.add(category1);
        categories.add(category2);
        
        stock.setCategories(categories);
        
        entityManager.persist(stock);
		entityManager.getTransaction().commit();

		System.out.println("Done");
	}
}
